"""
Case Type   : 数据库系统
Case Name   : 集群业务处理过程中，修改实例数据目录下pg_xlog目录权限，集群业务停止，报错
Create At   : 2020/06/29
Owner       : ningyali
Description :
    1、执行TPCC业务
    2、在业务执行过程中修改pg_xlog权限
    3、查看tpcc业务执行状态
    4、恢复pg_xlog权限
    5、重启主备
    6、再次执行TPCC
Expect      :
    1、开始执行TPCC业务
    2、在业务执行过程中修改pg_xlog权限000成功
    3、查看tpcc业务执行失败
    4、恢复pg_xlog权限成功
    5、重启主备成功
    6、再次执行TPCC成功
History     :
    modified: 2021/2/3 modified by @peilinqian,优化脚本逻辑
    modified: 2024/7/8 modified by @ningyali,优化用例解决不一致问题
"""

import os
import time
import unittest

from testcase.utils.ComThread import ComThread
from testcase.utils.Common import Common
from testcase.utils.CommonSH import *
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DbsysCase044(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('----Opengauss_Reliability_Dbsys_Case044.py start----')
        self.rootNode = Node()
        self.dbUserNode = Node(node='dbuser')
        self.com = Common()
        self.sh = CommonSH()
        self.constant = Constant()

    def test_delpermission_pgxlog(self):
        self.log.info('----1、执行TPCC----')
        tpcc_thread = ComThread(self.com.startTPCC,
                                args=(self.rootNode, macro.TPCC_PATH,))
        tpcc_thread.start()
        time.sleep(5)

        self.log.info('----2、在业务执行过程中修改pg_xlog权限----')
        chmod_cmd = 'chmod 000 ' + macro.PG_XLOG_PATH
        self.log.info(chmod_cmd)
        chmod_result = self.rootNode.sh(chmod_cmd).result()
        self.assertEqual(chmod_result, '')

        ls_cmd = f'ls -al {os.path.dirname(macro.PG_XLOG_PATH)}'
        self.log.info(ls_cmd)
        show_msg = self.rootNode.sh(ls_cmd).result()
        self.log.info(show_msg)

        self.log.info('----3、验证TPCC执行结果----')
        tpcc_thread.join(timeout=int(macro.TPCC_RUN_MAX_TIME) * 2)
        tpcc_result = tpcc_thread.get_result()
        self.log.info(tpcc_result)
        self.assertNotIn(self.constant.TPCC_SUCCESS_MSG, tpcc_result)

        self.log.info('----查询主备状态----')
        status = self.sh.getDbClusterStatus('status')
        self.assertFalse(status)

        self.log.info('----4、恢复pg_xlog权限----')
        chmod_cmd = 'chmod 700 ' + macro.PG_XLOG_PATH
        self.log.info(chmod_cmd)
        chmod_result = self.rootNode.sh(chmod_cmd).result()
        self.assertEqual(chmod_result, '')

        self.log.info("----5、重启集群----")
        self.sh.stopDbCluster()
        self.sh.startDbCluster()

        self.log.info('----查看主机query，同步是否正常----')
        self.sh.check_location_consistency('primary', 3)

        self.log.info('----查询主备状态----')
        status = self.sh.getDbClusterStatus('status')
        self.assertTrue(status)

        self.log.info('----6、再次执行TPCC----')
        tpcc_result = self.com.startTPCC(self.rootNode,
                                         macro.TPCC_PATH)
        self.log.info(tpcc_result)
        self.assertIn(self.constant.TPCC_SUCCESS_MSG, tpcc_result)

        self.log.info('----查看主机query，同步是否正常----')
        self.sh.check_location_consistency('primary', 3)

        self.log.info('----查询主备状态----')
        status = self.sh.getDbClusterStatus('status')
        self.assertTrue(status)

    def tearDown(self):
        self.log.info('----this is tearDown----')
        self.log.info('----恢复pg_xlog权限----')
        chmod_cmd = 'chmod 700 ' + macro.PG_XLOG_PATH
        self.log.info(chmod_cmd)
        chmod_result = self.rootNode.sh(chmod_cmd).result()
        self.assertEqual(chmod_result, '')
